package com.zhang.cicadatravels.main.travels;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zhang.cicadatravels.R;

import java.util.ArrayList;

/**
 * Created by dllo on 16/6/23.
 */
public class TravelsFragmentAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<TravelsFragmentListViewBean> beans;

    public void setBeans(ArrayList<TravelsFragmentListViewBean> beans) {
        this.beans = beans;
        notifyDataSetChanged();
    }

    public TravelsFragmentAdapter(Context context) {
        this.context = context;

    }


    @Override
    public int getCount() {
        return beans == null ? 0 : beans.size();
    }

    @Override
    public Object getItem(int position) {
        return beans == null ? null : beans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_fragment_travels_listview, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.title.setText(beans.get(position).getName());
        holder.startDate.setText(beans.get(position).getStart_date() + "/");
        holder.days.setText(String.valueOf(beans.get(position).getDays()) + "天,");
        holder.photosCount.setText(String.valueOf(beans.get(position).getPhotos_count()) + "张");
        Picasso.with(context).load(beans.get(position).getFront_cover_photo_url()).into(holder.imageView);
        return convertView;
    }


    class ViewHolder {
        ImageView imageView;
        TextView title;
        TextView startDate;
        TextView days;
        TextView photosCount;

        public ViewHolder(View view) {
            imageView = (ImageView) view.findViewById(R.id.fragment_travels_item_imageview);
            title = (TextView) view.findViewById(R.id.fragment_travels_item_title_tv);
            startDate = (TextView) view.findViewById(R.id.fragment_travels_item_startdate_tv);
            days = (TextView) view.findViewById(R.id.fragment_travels_item_days_tv);
            photosCount = (TextView) view.findViewById(R.id.fragment_travels_item_photoscount_tv);

        }

    }
}
